package com.one.blocks.rbac.exception;

import com.one.blocks.common.response.ResponseHelper;
import com.one.blocks.common.response.WebResponse;
import com.one.blocks.mvc.exception.GlobalExceptionHandler;
import com.one.blocks.security.enums.SecurityExceptionEnum;
import com.one.blocks.security.exception.AuthcException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author <a href="mailto:idler41@163.con">linfuxin</a> created on 2023-10-23 21:19:42
 */
@Slf4j
@RestControllerAdvice
public class RbacGlobalExceptionHandler extends GlobalExceptionHandler {

    /**
     * 未捕获权限异常
     *
     * @param e Exception
     * @return 统一格式异常响应
     */
    @ExceptionHandler(AccessDeniedException.class)
    public WebResponse<Void> accessDenied(AccessDeniedException e) {
        return ResponseHelper.fail(SecurityExceptionEnum.NO_PERMISSION);
    }

    /**
     * 未捕获认证异常
     *
     * @param e Exception
     * @return 统一格式异常响应
     */
    @ExceptionHandler(AuthcException.class)
    public WebResponse<Void> unauthorized(AuthcException e) {
        return ResponseHelper.fail(e.getCodeMsgEnum());
    }
}
